package com.tau.egonetwork.algorithms;

import java.util.HashMap;
import java.util.Map;

import com.tau.egonetwork.profiles.MainProfile;
import com.tau.egonetwork.profiles.Profile;

import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.graph.util.Pair;

public class GraphAdapter<T> {

	public static <T extends Comparable<T>> EgoNetworkGraph<T> getGraph(MainProfile<T> profile) {
		
		//create empty graph to be returned once full:
		//(sparse graph - undirectional graph with no parallel edges)
		EgoNetworkGraph<T> graph = new EgoNetworkGraph<T>();
		
		//add root:
		graph.addVertex(profile.getID());
		
		if (profile.getFriendsList() == null)
			return graph;

		int edgeCount = 0;
		
		//create vertices from friends:
		for (Profile<T> p : profile.getFriendsList()) {
			graph.addVertex(p.getID());
			//add edge between root and friend:
			graph.addEdge(edgeCount++, profile.getID(), p.getID(), EdgeType.UNDIRECTED);
		}
		
		Map<Pair<T>, Integer> edges = new HashMap<Pair<T>, Integer>();
		
		//go over all friends, if no edge representing friendship exists, add it:
		for (Profile<T> p : profile.getFriendsList()) {
			
			if (p.getFriendsList() == null)
				continue;
			
			for (Profile<T> pp : p.getFriendsList()) {
				
				if (!edges.containsKey(new Pair<T>(p.getID(), pp.getID()))) {
					graph.addEdge(edgeCount, p.getID(), pp.getID(), EdgeType.UNDIRECTED);
					edges.put(new Pair<T>(p.getID(), pp.getID()), edgeCount);
					edgeCount++;
				}
				
			}
			
		}
		
		return graph;
	}
	
}
